package com.webserver.servlet;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

public class ShowAllUser extends HttpServlet{

    public void service(HttpRequest httpRequest, HttpResponse httpResponse){
        System.out.println("咩咩咩咩咩咩咩咩咩咩咩");
        try (RandomAccessFile raf = new RandomAccessFile("user.dat","rw")){
            PrintWriter pw = httpResponse.printWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>我的首页</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>用户列表</h1>");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>用户名</td>");
            pw.println("<td>密码</td>");
            pw.println("<td>昵称</td>");
            pw.println("<td>年龄</td>");
            pw.println("<td>操作</td>");
            pw.println("</tr>");
            for(int i=0;i<raf.length()/100;i++){
                byte[] data = new byte[32];
                raf.read(data);
                String username = new String(data,"UTF-8").trim();
                raf.read(data);
                String password = new String(data,"UTF-8").trim();
                raf.read(data);
                String nickname = new String(data,"UTF-8").trim();
                int age = raf.readInt();
                pw.println("<tr>");
                pw.println("<td>"+username+"</td>");
                pw.println("<td>"+password+"</td>");
                pw.println("<td>"+nickname+"</td>");
                pw.println("<td>"+age+"</td>");
                pw.println("<td> <a href=\"./ToUpdateServlet?username="+username+"\">修改</a></td>");
                pw.println("</tr>");
            }
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
            httpResponse.setContentType("text/html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
